﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using babydealModel;

public partial class dn_them_khuyen_mai : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void them_km_Click(object sender, EventArgs e)
    {

        string str_ten_sp = ten_sp.Text;

        //Lấy id của thằng đang login
        int id_the_loai = Convert.ToInt32(the_loai.SelectedValue);
        string str_mo_ta_sp = mo_ta_sp.Text;
        string str_noi_dung = noi_dung.Text;
        string str_dk_ap_dung = dieu_kien_ap_dung.Text;
        string str_dac_trung = dac_trung.Text;
        int iGiaGoc = Convert.ToInt32(gia_goc.Text);
        int iGiaKM = Convert.ToInt32(gia_km.Text);
        DateTime dNgayBatDau = Convert.ToDateTime(ngay_bat_dau.Text);
        DateTime dNgayKetThuc = Convert.ToDateTime(ngay_ket_thuc.Text);
        int iSoLuongToiDa = Convert.ToInt32(so_luong_toi_da.Text);
        int iSoLuongToiThieu = Convert.ToInt32(so_luong_toi_thieu.Text);
        string str_anh_1 = null;
        string str_anh_2 = null;
        string str_anh_3 = null;
        string str_anh_4 = null;
        string str_anh_5 = null;

        string savePath = Server.MapPath("~/upload/");
        if (hinh_anh_1.HasFile)
        {
            string fileName = hinh_anh_1.FileName;
            if (System.IO.File.Exists(savePath + fileName))
            {
                int counter = 1;
                string pathToCheck = savePath + fileName;
                string tempFileName = fileName;
                while (System.IO.File.Exists(pathToCheck))
                {
                    // if a file with this name already exists,
                    // prefix the filename with a number.
                    tempFileName = counter.ToString() + "_" + fileName;
                    pathToCheck = savePath + tempFileName;
                    counter++;
                }

                fileName = tempFileName;
            }
            hinh_anh_1.SaveAs(savePath + fileName);
            str_anh_1 = fileName;
        }
        if (hinh_anh_2.HasFile)
        {
            string fileName = hinh_anh_2.FileName;
            if (System.IO.File.Exists(savePath + fileName))
            {
                int counter = 1;
                string pathToCheck = savePath + fileName;
                string tempFileName = fileName;
                while (System.IO.File.Exists(pathToCheck))
                {
                    // if a file with this name already exists,
                    // prefix the filename with a number.
                    tempFileName = counter.ToString() + "_" + fileName;
                    pathToCheck = savePath + tempFileName;
                    counter++;
                }

                fileName = tempFileName;
            }
            hinh_anh_2.SaveAs(savePath + fileName);
            str_anh_2 = fileName;
        }
        if (hinh_anh_3.HasFile)
        {
            string fileName = hinh_anh_3.FileName;
            if (System.IO.File.Exists(savePath + fileName))
            {
                int counter = 1;
                string pathToCheck = savePath + fileName;
                string tempFileName = fileName;
                while (System.IO.File.Exists(pathToCheck))
                {
                    // if a file with this name already exists,
                    // prefix the filename with a number.
                    tempFileName = counter.ToString() + "_" + fileName;
                    pathToCheck = savePath + tempFileName;
                    counter++;
                }

                fileName = tempFileName;
            }
            hinh_anh_3.SaveAs(savePath + fileName);
            str_anh_3 = fileName;
        }
        if (hinh_anh_4.HasFile)
        {
            string fileName = hinh_anh_4.FileName;
            if (System.IO.File.Exists(savePath + fileName))
            {
                int counter = 1;
                string pathToCheck = savePath + fileName;
                string tempFileName = fileName;
                while (System.IO.File.Exists(pathToCheck))
                {
                    // if a file with this name already exists,
                    // prefix the filename with a number.
                    tempFileName = counter.ToString() + "_" + fileName;
                    pathToCheck = savePath + tempFileName;
                    counter++;
                }

                fileName = tempFileName;
            }
            hinh_anh_4.SaveAs(savePath + fileName);
            str_anh_4 = fileName;
        }
        if (hinh_anh_5.HasFile)
        {
            string fileName = hinh_anh_5.FileName;
            if (System.IO.File.Exists(savePath + fileName))
            {
                int counter = 1;
                string pathToCheck = savePath + fileName;
                string tempFileName = fileName;
                while (System.IO.File.Exists(pathToCheck))
                {
                    // if a file with this name already exists,
                    // prefix the filename with a number.
                    tempFileName = counter.ToString() + "_" + fileName;
                    pathToCheck = savePath + tempFileName;
                    counter++;
                }

                fileName = tempFileName;
            }
            hinh_anh_5.SaveAs(savePath + fileName);
            str_anh_5 = fileName;
        }

        using (babydealEntities context = new babydealEntities())
        {
            babydealModel.khuyen_mai km = new babydealModel.khuyen_mai();
            km.ten_sp = str_ten_sp;
            km.id_doanh_nghiep = Convert.ToInt32(Session["IDDoanhNghiep"]);
            km.id_the_loai = id_the_loai;
            km.mo_ta_sp = str_mo_ta_sp;
            km.dac_trung = str_dac_trung;
            km.gia_goc = iGiaGoc;
            km.gia_km = iGiaKM;
            km.ngay_bat_dau = dNgayBatDau;
            km.ngay_ket_thuc = dNgayKetThuc;
            km.so_luong_toi_thieu = iSoLuongToiThieu;
            km.so_luong_toi_da = iSoLuongToiDa;
            km.da_duoc_duyet = 0;
            km.noi_dung = str_noi_dung;
            km.dieu_kien_ap_dung = str_dk_ap_dung;
            km.anh_1 = str_anh_1;
            km.anh_2 = str_anh_2;
            km.anh_3 = str_anh_3;
            km.anh_4 = str_anh_4;
            km.anh_5 = str_anh_5;

            context.AddTokhuyen_mai(km);
            context.SaveChanges();
        }
        Response.Redirect("./khuyen_mai.aspx");
    }
}